#pragma once

#include "GNSSObsRecordRead.hpp"

namespace ns_GNSSObsInput
{
    using namespace ns_Util;

#define ALL_GNSSTYPE {GNSSType::BDS, GNSSType::GPS}
    class GNSSObsInput
    {
    public:
        GNSSObsInput()
        {
        }
        static bool Start(const std::string &inJson)
        {
            Json::Reader reader;
            Json::Value inValue;

            if (!reader.parse(inJson, inValue))
            {
                return false;
            }

            std::string fileName = inValue["FileName"].asString();

            if (fileName.empty())
                return false;

            if (!FileUtil::IsFileExist(PathUtil::Src(fileName)))
            {
                Log(Debug) << "需要读取的文件不存在！" << '\n';
                return false;
            }

            if (FileUtil::IsFileExist(PathUtil::Dir(fileName)))
            {
                Log(Debug) << "文件已被读取" << '\n';
                return true;
            }

            return GNSSObsRecordReader::ReadRecordToFile(fileName, ALL_GNSSTYPE);
        }

        static bool RemoveTempFiles(const std::string &inJson)
        {
            Json::Reader reader;
            Json::Value inValue;

            if (!reader.parse(inJson, inValue))
            {
                return false;
            }

            std::string fileName = inValue["FileName"].asString();

            if (!FileUtil::RemoveFilesInDirectory(PathUtil::Dir(fileName)))
                return false;

            rmdir(PathUtil::Dir(fileName).c_str());

            return true;
        }
    
        static bool GetEpochRecord(const std::string& inJson,std::string* outJson)
        {
            Json::Reader reader;
            Json::Value inValue;

            if (!reader.parse(inJson, inValue))
            {
                return false;
            }

            std::string fileName = inValue["FileName"].asString();
            std::string curTimeString = JsonUtil::ValueToString(inValue["CurTime"]);
            Log(Debug)<<curTimeString<<'\n';
            TimeFactory::TimePtr curTime = TimeFactory::MakeTimePtr(TimeFactory::ValueType::GPSTimeSys);
            curTime->Decode(curTimeString);
            
            std::string s;
            curTime->Encode(&s);
            Log(Debug)<<s<<'\n';

            std::string filePath = PathUtil::ObsRecord(fileName,curTime->SecondFromRefTime());
            if(!FileUtil::IsFileExist(filePath))
                return false;
            
            FileUtil::ReadFromFile(filePath,outJson);
            return true;
        }
    };
}